search.parse();

var states = search.get('states');
var grid = search.get('datagrid');
var queryParams = new Form('query-params');

//加载功能按钮
var isrecycle = __param.recycle ? true : false;
var gridutil = new DatagridUtil(grid, {
	funcid : __param.funcid, 
	isRecycle : isrecycle, 
	validateRules : [
		{methodname : 'auditSupplier', conditions : [[5]], ismultiple : true}
	],
	validateOptions : {
		fields : ['entitystate'],
		ignoretypes : ['queryDetail']
	}
});

//页面加载完成执行
$(function() {
	states.loadData(sysGlobal.EntityStateForEntQuery);
	
	grid.url = __ctx + '/supplier/querySupplierPageData.xhtml';
	grid.load();
});

//查询
function doSearch() {
	grid.load(queryParams.getData());
}

//绘制表格单元格
function ondrawcell(e) {
	var field = e.column.field, record = e.record;
	switch (field) {
		case "buslicenseno":
			e.html = "<span class='grid-cell-link' onclick='showImage(\"" + record["buslicense"] + "\")' title='点击查看营业执照'>" + record[field] + "</span>";
			break;
		case "detailaddr":
			e.html = "<span class='grid-cell-link' onclick='showDistributionAreas(\"" + record["entityid"] + "\");' title='点击查看配送区域'>[" + 
				record["province"] + "]&nbsp;[" + record["city"] + "]&nbsp;[" + record["district"] + "]&nbsp;[" + record["street"] + "]</span><br/>" + 
				"<span class='grid-cell-link' onclick='showDistributionAreas(\"" + record["entityid"] + "\");' title='点击查看配送区域'>" + record[field] + "</span>";
			break;
		case "otherdata":
		{
			var html = "<span class='grid-cell-link' onclick='showImage(\"" + record["folicense"] + "\")' title='点击查看食品经营许可证'>食品经营许可证</span><br/>";
			if (record["testreport"]) html += "<span class='grid-cell-link' onclick='showImage(\"" + record["testreport"] + "\")' title='点击查看检测报告'>检测报告</span>&nbsp;&nbsp;";
			else html += "<span class='grid-cell-link-disabled' title='未提交此资料'>检测报告</span>&nbsp;&nbsp;";
			if (record["qualityreport"]) html += "<span class='grid-cell-link' onclick='showImage(\"" + record["qualityreport"] + "\")' title='点击查看质量报告'>质量报告</span>";
			else html += "<span class='grid-cell-link-disabled' title='未提交此资料'>质量报告</span>";
			
			e.html = html;
			break;
		}
		case "entitystate":
			e.html = "<span>" + parseState(record[field]) + "</span>";
			break;
		case "applytime":
		case "audittime":
			if (record[field]) e.html = "<span>" + new Date(record[field]).format("yyyy-MM-dd HH:mm:ss") + "</span>";
			break;
	}
}

//解析状态
function parseState(value) {
	var text = "", states = sysGlobal.EntityState;
	for (var i = 0 ; i < states.length; i++) {
		if (value == states[i].value) text = states[i].text;
	}
	return text;
}

//显示图片
function showImage(url) {
	var win = $(top.window);
	var data = {
		url : __ctx + "/viewimagedata.xhtml", 
		title : "图片资料", 
		width : win.width(), 
		height : win.height(), 
		onload : function(window) {
			window.loadImage(url);
		}, 
		ondestroy : function(result){
		}
	}
	top.search.popDialog(data);
}

//审核供应商
function auditSupplier(e) {
	top.search.audit({
		content : "请选择审核操作...", 
		btnl : "通过", 
		btnr : "退回", 
		funl : function() {
			updateSupplierState(e.ids, 6);
		}, 
		funr : function() {
			updateSupplierState(e.ids, 4);
		}
	});
}

//修改供应商状态
function updateSupplierState(entityids, state) {
	pcGlobal.sendRequest(__ctx + "/supplier/updateState.xhtml", {entityids : entityids, state : state}, function(data, status) {
		if (data.status) {
			search.info({content : "操作成功！", funl : function() {
				grid.reload();
			}});
		} else search.warn({content : "操作失败！"});
	});
}

//查看配送区域
function showDistributionAreas(entityid) {
	var data = {
		url : __ctx + "/supplier/viewDistributionArea.xhtml", 
		title : "配送区域", 
		width : 500, 
		height : 400, 
		onload : function(window) {
			window.loadData(entityid);
		}, 
		ondestroy : function(result){
		}
	}
	top.search.popDialog(data);
}

//查看详情
function queryDetail(e) {
	var data = {
		url : __ctx + "/supplier/viewSupplierDetail.xhtml", 
		title : "查看详情", 
		width : 820, 
		height : 600, 
		onload : function(window) {
			window.loadData(e.ids);
		}, 
		ondestroy : function(result){
		}
	}
	top.search.popDialog(data);
}
